﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DOTNET_ASSIGNMENT.Models;
using DOTNET_ASSIGNMENT.Services;
using PagedList;
using PagedList.Mvc;
using System.Web.Security;

namespace DOTNET_ASSIGNMENT.Controllers
{
    public class AdminFileController : Controller
    {
        private DataContext db = new DataContext();

        //
        // GET: /AdminFile/

        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }

            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            ViewBag.EmailSortParm = sortOrder == "Email" ? "email_desc" : "Email";
            ViewBag.BlockSortParm = sortOrder == "Blocked" ? "blocked_desc" : "Blocked";
            ViewBag.SizeSortParm = sortOrder == "Size" ? "size_desc" : "Size";

            FileServices fileServices = new FileServices();

            IEnumerable<DOTNET_ASSIGNMENT.Models.File> query = fileServices.GetAllFiles();

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            if (!String.IsNullOrEmpty(searchString))
            {
                query = query.Where(s => s.DisplayName.Contains(searchString));
            }

            switch (sortOrder)
            {
                case "Size":
                    query = query.OrderBy(s => s.Size);
                    break;
                case "size_desc":
                     query = query.OrderByDescending(s => s.Size);
                    break;
                case "Blocked":
                    query = query.OrderBy(s => s.IsBlocked);
                    break;
                case "blocked_desc":
                    query = query.OrderByDescending(s => s.IsBlocked);
                    break;
                case "name_desc":
                    query = query.OrderByDescending(s => s.DisplayName);
                    break;
                case "Date":
                    query = query.OrderBy(s => s.Date);
                    break;
                case "date_desc":
                    query = query.OrderByDescending(s => s.Date);
                    break;
                case "email_desc":
                    query = query.OrderByDescending(s => s.CustomerEmail);
                    break;
                case "Email":
                     query = query.OrderBy(s => s.CustomerEmail);
                    break;
                default:
                    query = query.OrderBy(s => s.DisplayName);
                    break;
            }

            int pageSize = 5;
            int pageNumber = (page ?? 1);
            return View(query.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /AdminFile/Details/5

        public ActionResult Details(int id = 0)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }

            File file = db.Files.Find(id);
            if (file == null)
            {
                return HttpNotFound();
            }
            return View(file);
        }

        //
        // GET: /AdminFile/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AdminFile/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(File file)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }

            if (ModelState.IsValid)
            {
                db.Files.Add(file);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(file);
        }

        //
        // GET: /AdminFile/Edit/5

        public ActionResult Edit(int id = 0)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }

            File file = db.Files.Find(id);
            if (file == null)
            {
                return HttpNotFound();
            }
            return View(file);
        }

        //
        // POST: /AdminFile/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(File file)
        {
            if (ModelState.IsValid)
            {
                db.Entry(file).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(file);
        }

        //
        // GET: /AdminFile/Delete/5

        public ActionResult Delete(int id = 0)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }

            File file = db.Files.Find(id);
            if (file == null)
            {
                return HttpNotFound();
            }
            return View(file);
        }

        //
        // POST: /AdminFile/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            File file = db.Files.Find(id);
            db.Files.Remove(file);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Block(string filename)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }

            FileServices services = new FileServices();
            services.BlockFile(filename);
            return RedirectToAction("Index");
        }

        public ActionResult Unblock(string filename)
        {
            if (Session["Email"]  == null || Session["Email"].ToString() != "Admin@057213.com")
            {
                FormsAuthentication.SignOut();
                Session.RemoveAll();
                return RedirectToAction("Login", "Account");
            }
            FileServices services = new FileServices();
            services.UnBlockFile(filename);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}